package com.zw.admin.server.service.impl;

import com.zw.admin.server.dao.DeviceDao;
import com.zw.admin.server.dao.SpaceDao;
import com.zw.admin.server.dao.SpaceDeviceDao;
import com.zw.admin.server.model.Device;
import com.zw.admin.server.model.Space;
import com.zw.admin.server.model.SpaceDevice;
import com.zw.admin.server.model.UserSpace;
import com.zw.admin.server.service.SpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class SpaceServiceImpl implements SpaceService {

    @Autowired
    private SpaceDao spaceDao;

    @Autowired
    private DeviceDao deviceDao;

    @Autowired
    private SpaceDeviceDao spaceDeviceDao;

    @Override
    public int saveOrUpdateSpace(Space space) {
        //找出Id主键
        String spaceId = space.getSpaceId();
        Space sqlResult = spaceDao.selectByPrimaryKey(spaceId);
        if (sqlResult != null) {
            //如果Id主键不为空，则为更新
            return spaceDao.updateByPrimaryKey(space);
        } else {
            //如果Id主键为空，则为新增
            return spaceDao.insert(space);
        }
    }

    @Override
    public int saveOrUpdateSpaceDevice(String spaceId, String deviceId) {

        Space space = spaceDao.selectByPrimaryKey(spaceId);
        Device device = deviceDao.selectByPrimaryKey(deviceId);
        if (space != null && device != null) {
            SpaceDevice spaceDevice = new SpaceDevice();
            spaceDevice.setDeviceId(device.getId());
            spaceDevice.setSpaceId(space.getSpaceId());
            return spaceDeviceDao.insert(spaceDevice);
        }
        return 0;
    }

    @Override
    public int saveOrUpdateUserSpace(Long userId, String spaceId) {
        UserSpace userSpace = new UserSpace();
        userSpace.setUserId(String.valueOf(userId));
        userSpace.setSpaceId(spaceId);
        return 0;
    }

    @Override
    public int countSpace(Map<String, Object> params) {
        String count = spaceDao.countSpace(params);
        return Integer.parseInt(count);
    }

    @Override
    public int countSpaceDevice(Map<String, Object> params) {
        return spaceDeviceDao.countSpaceDevice(params);
    }

    @Override
    public List<Space> spaceList(Map<String, Object> params, Integer offset, Integer limit) {
        if (params.get("spaceName").equals("")) {
            return spaceDao.listAll(params, offset, limit);
        } else {
            return spaceDao.listByName(params, offset, limit);
        }
    }

    @Override
    public List<Device> spaceDeviceList(Map<String, Object> params, Integer offset, Integer limit) {
        return spaceDeviceDao.listAll(params, offset, limit);
    }

    @Override
    public int delSpace(Long spaceId) {
        return spaceDao.deleteSpace(spaceId);
    }


    @Override
    public int deleteSpaceDeviceBySpaceId(Long spaceId) {
        return spaceDeviceDao.deleteSpaceDeviceBySpaceId(spaceId);
    }

    @Override
    public int deleteBySpaceDeviceId(Long deviceId) {
        return spaceDeviceDao.deleteBySpaceDeviceId(deviceId);
    }

    @Override
    public int editSpaceName(Long spaceId, String spaceName) {
        Space space = spaceDao.selectByPrimaryKey(spaceId);
        space.setSpaceName(spaceName);
        spaceDao.updateByPrimaryKey(space);
        return 1;
    }

    @Override
    public List<Space> getTopNode() {
        return spaceDao.getTopNode();
    }

    @Override
    public List<Space> getSubNode(String spaceId) {
        return spaceDao.getSubNode(spaceId);
    }

    @Override
    public ArrayList<String> getAllSubId(String spaceId) {
        ArrayList<String> stack = new ArrayList<String>();
        ArrayList<String> spaceIds = new ArrayList<String>();

        //将父Id加入栈中,将父id加入spaceIds中用于返回
        stack.add(spaceId);
        spaceIds.add(spaceId);
        //遍历栈
        while (stack.size() != 0) {
            String id = stack.get(0);
            List<Space> subSpace = getSubNode(id);
            for (Space s : subSpace) {
                stack.add(s.getSpaceId());
                spaceIds.add(s.getSpaceId());
            }
            stack.remove(0);
        }

        return spaceIds;
    }

    @Override
    public List<Device> getSpaceDevice(ArrayList<String> spaceIds) {
        List<String> deviceIds = spaceDao.getSpaceDevice(spaceIds);
        List<Device> deviceList = new ArrayList<Device>();
        for (String id : deviceIds) {
            deviceList.add(deviceDao.selectByIotId(id));
        }
        return deviceList;
    }

    @Override
    public int delSpaceDevice(String spaceId, List<String> deviceIds) {
        for (String deviceId : deviceIds) {
            spaceDeviceDao.delBySpaceIdAndDeviceId(spaceId, deviceId);
            //enable = 0 代表未绑定空间
            Device device = deviceDao.selectByPrimaryKey(deviceId);
            device.setIsEnable(0);
            deviceDao.updateByPrimaryKey(device);
        }
        return 1;
    }

    @Override
    public int addSpaceDevice(String spaceId, List<String> deviceIds) {
        for (String deviceId : deviceIds) {
            spaceDeviceDao.addBySpaceIdAndDeviceId(spaceId, deviceId);
            //enable = 1 代表已经绑定空间
            Device device = deviceDao.selectByPrimaryKey(deviceId);
            device.setIsEnable(1);
            deviceDao.updateByPrimaryKey(device);
        }
        return 0;
    }

    @Override
    public List<String> getSpaceDeviceIds(Long spaceId) {

        return spaceDeviceDao.getSpaceDeviceIds(spaceId);
    }

    @Override
    public List<Device> getUnableDevice() {
        return deviceDao.getUnableDevice();
    }


}
